package com.mingserve.raw.listener;

import com.alibaba.fastjson2.JSONObject;
import com.mingserve.common.core.text.Convert;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

/**
 * 格式化传感器配置
 *
 * @author lvwshuai
 * @date 2025-02-26
 */
public class DataFormatter {

    public static <T extends Map<String, Object>> T formatData(T map, Supplier<T> mapSupplier) {
        T obj = mapSupplier.get();
        map.forEach((key, value) -> {
            if ("SensorID".equals(key) || "Otaurl".equals(key)) {
                obj.put(key, value);
            } else if ("RmsThreshold".equals(key)) {
                obj.put(key, Convert.toFloat(value));
            } else if ("SamplePoints".equals(key)) {
//                obj.put(key, Integer.highestOneBit(Convert.toInt(value))); // 转为2的幂指数值
                obj.put(key, Convert.toInt(value));
            }
            else {
                obj.put(key, Convert.toInt(value));
            }
        });
        return obj;
    }

    public static JSONObject formatObj(JSONObject map) {
        return formatData(map, JSONObject::new);
    }

    public static Map<String, Object> formatMap(Map<String, Object> map) {
        return formatData(map, HashMap::new);
    }
}